<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>保险公司 管理</title>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>
<body class="iframeBody">
<form id="insCompInfo_form">
    <div class="fieldset1 tabel_col tabel_col_1 clearFix">
        <div class="caption">
            <span class="title">保险支公司基本信息</span>
        </div>
        <div class="fieldBox col_50">
            <div class="label">保险公司</div>
            <div class="field">${insCompInfoVO.compPidNm}</div>
        </div>
        <div class="fieldBox col_50">
            <div class="label">支公司名称</div>
            <div class="field">${insCompInfoVO.compNm}</div>
        </div>

        <div class="fieldBox col_50">
            <div class="label">区域-省份</div>
            <div class="field">${insCompInfoVO.rgnPrNm}</div>
        </div>

        <div class="fieldBox col_50">
            <div class="label">区域-城市</div>
            <div class="field">${insCompInfoVO.rgnCyNm}</div>
        </div>

        <div class="fieldBox col_50">
            <div class="label">购买方式</div>
            <div class="field">${insCompInfoVO.buyWayCdNm}</div>
        </div>

        <div class="fieldBox col_50">
            <div class="label">渠道</div>
            <div class="field">${insCompInfoVO.chnCdNm}</div>
        </div>

        <div class="fieldBox col_50 chn1">
            <div class="label">渠道起始日期</div>
            <div class="field">${insCompInfoVO.chnBgDts}</div>
        </div>
        <div class="fieldBox col_50 chn1">
            <div class="label">渠道结束日期</div>
            <div class="field">${insCompInfoVO.chnEndDts}</div>
        </div>
        <div class="fieldBox col_50 chn1">
            <div class="label">联系人</div>
            <div class="field">${insCompInfoVO.lmNm}</div>
        </div>
        <div class="fieldBox col_50 chn1">
            <div class="label">联系电话</div>
            <div class="field">${insCompInfoVO.lmTel}</div>
        </div>
        <div class="fieldBox col_50 chn1">
            <div class="label">开户银行</div>
            <div class="field">${insCompInfoVO.bkNm}</div>
        </div>
        <div class="fieldBox col_50 chn1">
            <div class="label">开户行号</div>
            <div class="field">${insCompInfoVO.bkNo}</div>
        </div>
        <div class="fieldBox col_50 chn1">
            <div class="label">账户名称</div>
            <div class="field">${insCompInfoVO.accNm}</div>
        </div>
        <div class="fieldBox col_50 chn1">
            <div class="label">银行账号</div>
            <div class="field">${insCompInfoVO.accNo}</div>
        </div>
        <div class="fieldBox col_50 chn1">
            <div class="label">协议号</div>
            <div class="field">${insCompInfoVO.protNo}</div>
        </div>
        <div class="fieldBox col_50 chn1">
            <div class="label"></div>
            <div class="field">

            </div>
        </div>

        <div class="fieldBox col_100">
            <div class="label">备注</div>
            <div class="field">${insCompInfoVO.compRem}</div>
        </div>
        <c:if test="${not empty insCompInfoVO.id}">
            <div class="fieldBox col_100" id="fileModify">
                <div class="label">资料</div>
                <div class="field">
                    <jsp:include page="../../system/sysUlFilesRec/sysUlFileRecList.jsp" flush="true">
                        <jsp:param name="pkId" value="${insCompInfoVO.id}"/>
                        <jsp:param name="tabNm" value="INS_COMP_INFO"/>
                    </jsp:include>
                </div>
            </div>
        </c:if>
    </div>

    <div class="sqPanel">
        <div class="sqPanel_head inL">
            <div id="editDiv" class="btnsBox inLC">
                <span id="addPolBtn" class="sq-button sq-button-round">添加</span>
                <span id="delPolBtn" class="sq-button sq-button-round co_normal">删除</span>
            </div>
            <span>渠道政策</span>
        </div>
        <div class="sqPanel_body">
            <div id="ligergrid1_pol" class="sq-grid"></div>
        </div>
    </div>

    <div class="btnsBox" style="text-align: center;">
        <input type="text" style="display: none;" id="prInstId" name="prInstId" value="${prInstId}">
        <span id="startFlow_Btn" class="sq-button sq-button-round" style="display: none;">发起流程</span>
        <span id="saveInsCompInfo_Btn" class="sq-button sq-button-round" style="display: none;">保存</span>
        <span id="back_Btn" class="sq-button sq-button-round">返回</span>
    </div>
</form>
<script>
    var polGrid = null;
    $(function () {
        var insCompInfoId = "${insCompInfoVO.id}";//主键ID
        var chnCd = "${insCompInfoVO.chnCd}";//渠道
        if (insCompInfoId && chnCd == "1") {
            $(".chn1").show();
        } else {
            $(".chn1").hide();
        }
        if ("${flag}" == "0") {//申请
            $('#startFlow_Btn').show();
            $('#saveInsCompInfo_Btn').hide();
        }else if("${flag}" == "1"){//修改
            $('#startFlow_Btn').hide();
            $('#saveInsCompInfo_Btn').show();
        }else{//2查看
            $('#editDiv').hide();
            $('#startFlow_Btn').hide();
            $('#saveInsCompInfo_Btn').hide();
        }
        var prdTypList = [];//保险产品类型
        var insTypList = [];//保险种类
        var rebNatList = [];//返利性质
        //获取产品类型、保险种类、返利性质
        $.ajax({
            type: 'get',
            dataType: 'json',
            async:false,
            url: '<%=path%>/insurance/insCompChnPol/getInsDictList.do',
            success: function (msg) {
                if (msg.success == 1) {
                    prdTypList = msg.data.prdTypCdList;
                    insTypList = msg.data.insTypCdList;
                    rebNatList = msg.data.rebNatCdList;
                }
            }
        });

        polGrid = $("#ligergrid1_pol").ligerGrid({
            columns: [
                {display: '支付表号', name: 'paySchNo', width: 170, align: 'left',
                    editor: {
                        type: 'popup',
                        condition: {
                            fields: [
                                { name: 'cstNm', type: 'text', label: '客户', width: 200 },
                                { name: 'paySchNo', type: 'text', label: '支付表号', width: 200, newline:false}
                            ]
                        },
                        valueField: 'paySchNo', textField: 'paySchNo',
                        grid: {
                            url:'<%=path%>/insurance/insCompInfo/selectPrjPrdList.do',
                            method:'get',
                            columns: [
                                /*{display: '项目产品编号', name: 'prjPrdDtlId', minWidth: 100},*/
                                {display: '支付表编号', name: 'paySchNo', width: 175},
                                {display: '项目名称', name: 'prjNm', minWidth: 100},
                                {display: '客户名称', name: 'cstNm', minWidth: 50},
                                {display: '产品名称', name: 'prdNm', minWidth: 50},
                            ],
                            pageSize: 10,
                            isScroll: true,
                            checkbox: true,
                            isSingleCheck:true,
                            root: 'rows',
                            record: 'total',
                            width: '100%',
                            height:'300',
                        },
                        onSelected:function(e){
                            if (!e.data || !e.data.length) return;
                            var grid = liger.get("ligergrid1_pol");
                            var selected = e.data[0];
                            grid.updateRow(grid.lastEditRow.__id, {
                                prjPrdId: selected.prjPrdDtlId,
                                paySchNo: selected.paySchNo,
                                deposit: selected.deposit,
                            });
                        }
                    },
                },
                {
                    display: '项目产品明细ID', name: 'prjPrdId', width: 10, align: 'left',hide:'true',
                },
                {
                    display: '保险保证金', name: 'deposit', width: 10, align: 'left',hide:'true',
                },
                {
                    display: '保险产品类型', name: 'prdTypCd', width: 100, align: 'left',
                    valueField: 'prdTypCd',
                    textField: 'prdTypCdNm',
                    editor: {
                        type: 'select',
                        data: prdTypList,
                        valueField: 'key',
                        textField: 'value'
                    },
                    render: function (item) {
                        return item.prdTypCdNm;
                    }
                },
                {
                    display: '使用控制', name: 'insTypCd', width: 100, align: 'left',
                    valueField: 'insTypCd',
                    textField: 'insTypCdNm',
                    editor: {
                        type: 'select',
                        data: insTypList,
                        valueField: 'key',
                        textField: 'value',
                        isShowCheckBox: true,
                        isMultiSelect: false
                    },
                    render: function (item) {
                        return item.insTypCdNm;
                    }
                },
                {
                    display: '返利性质', name: 'rebNatCd', width: 100, align: 'left',
                    valueField: 'rebNatCd',
                    textField: 'rebNatCdNm',
                    editor: {
                        type: 'select',
                        data: rebNatList,
                        valueField: 'key',
                        textField: 'value',
                        onSelected:function(value,text){
                            if(value==2){
                                polGrid.updateRow(polGrid.lastEditRow.__id, {
                                    rebAmtDou: polGrid.lastEditRow.deposit,
                                });
                            }else{
                                polGrid.updateRow(polGrid.lastEditRow.__id, {
                                    rebAmtDou: 0,
                                });
                            }
                        }
                    },
                    render: function (item) {
                        return item.rebNatCdNm;
                    }
                },
                {
                    display: '返利金额', name: 'rebAmtDou', width: 100, align: 'right',
                    editor: {type: 'number',precision:2,toFix:true,
                        onToFix:function(){
                            top.$.ligerDialog.SQalert({SQ_type: "warning",content: "小数点后面最多"+feeScale+"小数！"});
                        }
                    },
                    render:function(rowdata,index,value){
                        return FormatMoney(rowdata.rebAmtDou);
                    }
                },
                {
                    display: '返利时间', name: 'rebDt', width: 100, render: renderDate, align: 'left',
                    type: 'date', format: 'yyyy-MM-dd', editor: {type: 'date'}
                },
                {
                    display: '备注', name: 'chnPolRem', minWidth: 100, align: 'left',isEllipsis: false,
                    type: 'text',editor: {type: 'text'}
                },
            ],
            onBeforeEdit: function (e) {
                //根据行数据里的"editable"属性，指定是否可以编辑这一行数据
                if (e.column.columnname == "rebAmtDou") {
                    if (e.record.editable == 0 && e.record.rebAmtDou!=0) {
                        e.column.editor.readonly = true;
                    }else{
                        e.column.editor.readonly = false;
                    };
                }
            },
            onBeforeSubmitEdit: function (e) {
                //获取下拉框的"flag"属性值, 并根据它设置editable
                if(e.column.columnname == "rebNatCd" && e.selected&& e.value==2){
                    e.record.editable = 0;
                }else if(e.column.columnname == "rebNatCd" && e.selected&& e.value==1){
                    e.record.editable = 1;
                }

                /*if(e.selected && e.column.columnname=='rebNatCd' && e.value==2){
                    e.record['rebAmtDou']=e.record['deposit'];
                }else if(e.selected && e.column.columnname=='rebNatCd' && e.value==1){
                    e.record['rebAmtDou']=0;
                }*/
            },
            usePager: false,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            height: 300,
            heightDiff: -2,
            enabledEdit: true,
            isScroll: false,
            checkbox: true,
            rownumbers: true,
            allowUnSelectRow: true,
            alternatingRow: true
        });

        <c:if test="${not empty chnPolList}">
            var jsonObj = {};
            jsonObj.rows = ${chnPolList};
            polGrid.set({data: jsonObj});
        </c:if>

        /* 新增 */
        $("#addPolBtn").click(function (event) {
            polGrid.addRow({
                prdTypCd: '',
                insTypCd: '',
                rebNatCd: '',
                rebAmt: 0,
                rebDt: new Date(),
                insTypCdNm: '',
                prjPrdId:null,
                prjNm:''
            });
        });

        /* 删除 */
        $("#delPolBtn").click(function (event) {
            polGrid.deleteSelectedRow();
        });

        /* 保存修改 */
        $("#saveInsCompInfo_Btn").click(function (event) {
            $(document).trigger('click');
            var data = polGrid.getData();
            if(data.length==0){
                $.ligerDialog.SQalert({content: "请先填写渠道政策", SQ_type: "warning"});
                return;
            }
            var prInstId=$('#prInstId').val();
            var wait_dialog;
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: {data: JSON.stringify(data),'prInstId':prInstId},
                url: '<%=path%>/insurance/insCompChnPol/addCompChnPol.do?insCompId=' + insCompInfoId + '&chnCd=' + chnCd,
                beforeSend: function () {
                    wait_dialog = $.ligerDialog.SQalert({
                        content: LogUtils.LOG_CMM_DATA_LOADING,
                        SQ_type: "waitting"
                    });
                },
                success: function (msg) {
                    wait_dialog.close();
                    if (msg.success == 1) {
                        top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                    } else {
                        top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                }
            });
        });

        //发起流程
        $("#startFlow_Btn").click(function (event) {
            $(document).trigger('click');
            var data = polGrid.getData();
            if(data.length==0){
                $.ligerDialog.SQalert({content: "请先填写渠道政策", SQ_type: "warning"});
                return;
            }
            var wait_dialog;
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: {data: JSON.stringify(data)},
                url: '<%=path%>/insurance/insCompChnPol/chnPolApply.do?insCompId=' + insCompInfoId + '&chnCd=' + chnCd,
                beforeSend: function () {
                    wait_dialog = $.ligerDialog.SQalert({
                        content: LogUtils.LOG_CMM_DATA_LOADING,
                        SQ_type: "waitting"
                    });
                },
                success: function (msg) {
                    wait_dialog.close();
                    if (msg.success == 1) {
                        top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "success",SQ_showButtons : true});
                        Remove_navTab();
                    } else {
                        top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "error",SQ_showButtons : true});
                    }
                }
            });
        });

        //返回
        $("#back_Btn").click(function(){
            Remove_navTab();
        });

    });

    function getUlFileRecDelColumn(){}

    //格式化时间
    function renderDate(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
        }
        return dateStr;
    }

    //扩展一个 数字输入 的编辑器
    $.ligerDefaults.Grid.editors['numberbox'] = {
        create: function (container, editParm) {
            var column = editParm.column;
            var precision = column.editor.precision;
            var input = $("<input type='text' style='text-align:right' class='l-text' />");
            input.bind('keypress', function (e) {
                var keyCode = window.event ? e.keyCode : e.which;
                return keyCode >= 48 && keyCode <= 57 || keyCode == 46 || keyCode == 8;
            });
            input.bind('blur', function () {
                var value = input.val();
                input.val(parseFloat(value).toFixed(precision));
            });
            container.append(input);
            return input;
        },
        getValue: function (input, editParm) {
            return parseFloat(input.val());
        },
        setValue: function (input, value, editParm) {
            if (!value) {
                input.val('');
                return;
            }
            var column = editParm.column;
            var precision = column.editor.precision;
            input.val(value.toFixed(precision));
        },
        resize: function (input, width, height, editParm) {
            input.width(width).height(height);
        }
    };

    //扩展 numberbox 类型的格式化函数
    $.ligerDefaults.Grid.formatters['numberbox'] = function (value, column) {
        if (!value) value = 0;
        var precision = column.editor.precision;
        return formatCurrency(value.toFixed(precision));
    };

    /**
     * 格式化金额
     */
    function formatCurrency(num) {
        if (!num) return "0.00";
        num = num.toString().replace(/\$|\,/g, '');
        if (isNaN(num))
            num = "0.00";
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num * 100 + 0.50000000001);
        cents = num % 100;
        num = Math.floor(num / 100).toString();
        if (cents < 10)
            cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
            num = num.substring(0, num.length - (4 * i + 3)) + ',' +
                    num.substring(num.length - (4 * i + 3));
        return (((sign) ? '' : '-') + '' + num + '.' + cents);
    }
</script>
</body>
</html>
